package com.njlime.core.sdk.wechat.user;

import com.alibaba.fastjson.JSONObject;
import com.njlime.core.sdk.wechat.WeChatUtil;
import com.njlime.core.sdk.wechat.jsapi.AccessToken;
import com.njlime.member.entiies.TMember;
import com.njlime.sys.entities.UnitWxConfig;
import com.njlime.sys.security.util.UserUtils;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * 通过网页授权获取的用户标签
 * @author Herman.Xiong
 * @date 2016年1月13日15:09:29
 */
public class UserLable {
	static Logger logger = Logger.getLogger(WeChatUtil.class);

	/**
	 * 创建用户组
	 * @param unitId 单位id
	 * @param name 组名称
	 * @return
	 */
	public static int createLable( int unitId,String name) {
		try {
			// 获取单位公共号信息信息
			UnitWxConfig config = UserUtils.getUnitConfig(unitId);
			if (config != null) {
				AccessToken token = WeChatUtil.getAccessToken(config.getApp_id(), config.getApp_secret());
				System.out.println(token.getToken());
				if (token != null) {
					logger.info(token.getToken());
					String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=" + token.getToken();
					String data = "{\"tag\" : {\"name\" : \""+name+"\"}}";
					JSONObject json = WeChatUtil.httpRequest(requestUrl, "POST", data);
					if (json.get("tag") != null) {
						JSONObject tag = (JSONObject) json.get("tag");
						return Integer.parseInt(tag.get("id").toString());
					}
					token.getToken();
				}
			}
		}catch (Exception e){
			logger.error(e);
		}
		return -1;
	}

	/**
	 * 修改用户组
	 * @param unitId 单位id
	 * @param name 组名称
	 * @param groupId 组id
	 * @return
	 */
	public static int editLable( int unitId,String name,int groupId ) {
		try {
			// 获取单位公共号信息信息
			UnitWxConfig config = UserUtils.getUnitConfig(unitId);
			if (config != null) {
				AccessToken token = WeChatUtil.getAccessToken(config.getApp_id(), config.getApp_secret());
				if (token != null) {
					logger.info(token.getToken());
					String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=" + token.getToken();
					String data = "{\"tag\":{\"id\":" + groupId + ",\"name\":\""+name+"\"}}";
					JSONObject json = WeChatUtil.httpRequest(requestUrl, "POST", data);
					logger.error("tage_update="+data+"_"+json);
					if (json.get("errcode") != null) {
						if(json.get("errcode").toString().equals("0")) {
							return 1;
						}
					}
					token.getToken();
				}
			}
		}catch (Exception e){
			logger.error("",e);
		}
		return -1;
	}

	/**
	 * 用户分组
	 * @param unitId 单位id
	 * @param list 用户id集合
	 * @param groupId 组id
	 * @return
	 */
	public static int batchtagging(int unitId, List<TMember> list, int groupId ) {
		try {
			// 获取单位公共号信息信息
			UnitWxConfig config = UserUtils.getUnitConfig(unitId);
			if (config != null) {
				AccessToken token = WeChatUtil.getAccessToken(config.getApp_id(), config.getApp_secret());
				if (token != null) {
					logger.info(token.getToken());
					String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=" + token.getToken();
					String data = "{  \"openid_list\" : [  ";
					if(list!=null && list.size()>0){
						for(TMember member :list ){
							data += "\""+ member.getOpenid() +"\"";
						}
					}
					data += "],  \"tagid\" : "+groupId+"}";
					JSONObject json = WeChatUtil.httpRequest(requestUrl, "POST", data);
					logger.info("batchtagging:"+data+"_"+json);
					if (json.get("errcode") != null) {
						if(json.get("errcode").toString().equals("0")) {
							return 1;
						}
					}
					token.getToken();
				}
			}
		}catch (Exception e){
			logger.error(e);
		}
		return -1;
	}

	public static int deleteLable(int unitId, String groupId) {
		try {
			// 获取单位公共号信息信息
			UnitWxConfig config = UserUtils.getUnitConfig(unitId);
			if (config != null) {
				AccessToken token = WeChatUtil.getAccessToken(config.getApp_id(), config.getApp_secret());
				if (token != null) {
					logger.info(token.getToken());
					String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=" + token.getToken();
					String data = "{\"tag\":{\"id\":" + groupId + "}}";
					JSONObject json = WeChatUtil.httpRequest(requestUrl, "POST", data);
					logger.error("tage_update="+data+"_"+json);
					if (json.get("errcode") != null) {
						if(json.get("errcode").toString().equals("0")) {
							return 1;
						}
					}
					token.getToken();
				}
			}
		}catch (Exception e){
			logger.error("",e);
		}
		return -1;

	}
}
